unit srvMain; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
  lNetComponents, Menus, StdCtrls, ExtCtrls, lNet, ComCtrls; 

type
  
  { TForm1 }

  TForm1 = class ( TForm ) 
    Net: TLTCPComponent;
    MainMenu1: TMainMenu;
    MemoText: TMemo;
    MenuItem1: TMenuItem;
    mnListen: TMenuItem;
    MenuItem3: TMenuItem;
    mnExit: TMenuItem;
    PlayTimer: TTimer;
    StatusBar1: TStatusBar;
    Player1Timer: TTimer;
    Player2Timer: TTimer;
    procedure FormCreate ( Sender: TObject ) ;
    procedure mnExitClick ( Sender: TObject ) ;
    procedure mnListenClick ( Sender: TObject ) ;
    procedure NetAccept ( aSocket: TLSocket ) ;
    procedure NetConnect(aSocket: TLSocket);
    procedure NetDisconnect(aSocket: TLSocket);
    procedure NetError(const msg: string; aSocket: TLSocket);
    procedure NetReceive(aSocket: TLSocket);
    procedure Player1TimerTimer ( Sender: TObject ) ;
    procedure Player2TimerTimer ( Sender: TObject ) ;
    procedure PlayTimerTimer ( Sender: TObject ) ;
    Reconstruct(MessageType: TMessageType; S : String); 
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form1: TForm1; 

implementation

{ TForm1 }


// ============================================================================
// Network functions
//

procedure TForm1.NetAccept ( aSocket: TLSocket ) ;
begin
  MemoText.Append('Connection accepted from: ' + aSocket.PeerAddress + ': ' + inttostr(aSocket.PeerPort));
  MemoText.SelStart := Length(MemoText.Lines.Text);
end;

procedure TForm1.NetConnect(aSocket: TLSocket);
begin
  // There shouldn't be any connections "from" the server
end;

procedure TForm1.NetDisconnect(aSocket: TLSocket);
begin
  MemoText.Append('Connection lost from: ' + aSocket.PeerAddress + ': ' + inttostr(aSocket.PeerPort));
  MemoText.SelStart := Length(MemoText.Lines.Text);
end;

procedure TForm1.NetError(const msg: string; aSocket: TLSocket);
begin
  MemoText.Append('Error: ' + Msg);
  MemoText.Append('from: ' + aSocket.PeerAddress + ': ' + inttostr(aSocket.PeerPort));
  MemoText.SelStart := Length(MemoText.Lines.Text);
end;





procedure TForm1.NetReceive(aSocket: TLSocket);

function Reassemble(const s: string): Integer; // how many bytes were processed
var
 tmp: string;
 MsgSize: Integer; // the "message size" 4 bytes int
 //ObjectType: Byte; // this is used to hold the "type" byte
 Mt : TMessageType;
begin
 Result := 0;                        // assume we fail first

 tmp := s;

 move(tmp,mt,Sizeof(mt));

 case mt of
  mtSphereInfo : if length(tmp) < TSphereInfoSize  then exit;
  mtBool       : if length(tmp) < TBoolMessageSize then exit;
  mtOwn        : if length(tmp) < TOwnMessageSize  then exit;
 end;

 Reconstruct(mt,tmp);

 Result := Length(tmp);          // we processed the raw data + our size and type header
end; 

begin
  if aSocket.GetMessage(S) > 0 then
    Reassemble(S);          
end;

procedure TForm1.Reconstruct(MessageType: TMessageType; S : String);   

begin

end; 
     
           
           
// ============================================================================
// Game Functions
// 
procedure TForm1.PlayTimerTimer ( Sender: TObject ) ;
begin
  // Stop the game, set Peeking true 
end;


procedure TForm1.Player2TimerTimer ( Sender: TObject ) ;
begin
  // Player 2 Drops whatever object he has 
end;


procedure TForm1.Player1TimerTimer ( Sender: TObject ) ;
begin
  // Player 1 Drops whatever object he has 
end; 
      
      







// ============================================================================
// Admin functions
//
procedure TForm1.FormCreate ( Sender: TObject ) ;
begin

  if Net.Listen(4665) then begin
    MemoText.Append('Accepting connections');
  end; 
  
  // Show the Current IP in the statusbar
  // TODO : ^^^^
end;

procedure TForm1.mnListenClick ( Sender: TObject ) ;
begin
  // This should be happening on formcreate, but you can still manually start it
  if Net.Listen(4665) then begin
    MemoText.Append('Accepting connections');
  end;
end;

procedure TForm1.mnExitClick ( Sender: TObject ) ;
begin
  Application.Terminate; 
end;




initialization
  {$I srvmain.lrs}

end.

